home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / common / warp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-02-13  |  56.2 KB  |  1,740 lines

  1. /* Warp  --- image filter plug-in for The Gimp image manipulation program
  2.  * Copyright (C) 1997 John P. Beale
  3.  * Much of the 'warp' is from the Displace plug-in: 1996 Stephen Robert Norris
  4.  * Much of the 'displace' code taken in turn  from the pinch plug-in 
  5.  *   which is by 1996 Federico Mena Quintero
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * You can contact me (the warp author) at beale@best.com
  22.  * Please send me any patches or enhancements to this code.
  23.  * You can contact the original The Gimp authors at gimp@xcf.berkeley.edu
  24.  *
  25.  * --------------------------------------------------------------------
  26.  * Warp Program structure: after running the user interface and setting the
  27.  * parameters, warp generates a brand-new image (later to be deleted 
  28.  * before the user ever sees it) which contains two grayscale layers,
  29.  * representing the X and Y gradients of the "control" image. For this
  30.  * purpose, all channels of the control image are summed for a scalar
  31.  * value at each pixel coordinate for the gradient operation.
  32.  *
  33.  * The X,Y components of the calculated gradient are then used to displace pixels
  34.  * from the source image into the destination image. The displacement vector is
  35.  * rotated a user-specified amount first. This displacement operation happens
  36.  * iteratively, generating a new displaced image from each prior image. 
  37.  * -------------------------------------------------------------------
  38.  *
  39.  * Revision History:
  40.  * Version 0.37  12/19/98 Fixed Tooltips and freeing memory
  41.  * Version 0.36  11/9/97  Changed XY vector layers  back to own image
  42.  *               fixed 'undo' problem (hopefully)
  43.  *
  44.  * Version 0.35  11/3/97  Added vector-map, mag-map, grad-map to
  45.  *               diff vector instead of separate operation
  46.  *               further futzing with drawable updates
  47.  *               starting adding tooltips
  48.  *
  49.  * Version 0.34  10/30/97   'Fixed' drawable update problem
  50.  *               Added 16-bit resolution to differential map
  51.  *             Added substep increments for finer control
  52.  *
  53.  * Version 0.33  10/26/97   Added 'angle increment' to user interface
  54.  *
  55.  * Version 0.32  10/25/97   Added magnitude control map (secondary control)
  56.  *               Changed undo behavior to be one undo-step per warp call.
  57.  *
  58.  * Version 0.31  10/25/97   Fixed src/dest pixregions so program works
  59.  *               with multiple-layer images. Still don't know
  60.  *               exactly what I did to fix it :-/  Also, added 'color' option for
  61.  *               border pixels to use the current selected foreground color.
  62.  *
  63.  * Version 0.3   10/20/97  Initial release for Gimp 0.99.xx
  64.  */
  65.  
  66. #include "config.h"
  67.  
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <unistd.h>
  71. #include <signal.h>
  72. #include <time.h>                  /* time(NULL) for random # seed */
  73.  
  74. #include <gtk/gtk.h>
  75.  
  76. #include <libgimp/gimp.h>
  77. #include <libgimp/gimpui.h>
  78.  
  79. #include "libgimp/stdplugins-intl.h"
  80.  
  81.  
  82. /* Some useful macros */
  83.  
  84. #define ENTRY_WIDTH     75
  85. #define TILE_CACHE_SIZE 30  /* was 48. There is a cache flush problem in GIMP preventing sequential updates */
  86. #define MIN_ARGS         6  /* minimum number of arguments required */
  87.  
  88. enum
  89. {
  90.   WRAP,
  91.   SMEAR,
  92.   BLACK,
  93.   COLOR
  94. };
  95.  
  96. typedef struct
  97. {
  98.   gdouble amount;
  99.   gint    warp_map;
  100.   gint    iter;
  101.   gdouble dither;
  102.   gdouble angle;
  103.   gint    wrap_type;
  104.   gint    mag_map;
  105.   gint    mag_use;
  106.   gint    substeps;
  107.   gint    grad_map;
  108.   gdouble grad_scale;
  109.   gint    vector_map;
  110.   gdouble vector_scale;
  111.   gdouble vector_angle;
  112. } WarpVals;
  113.  
  114. typedef struct
  115. {
  116.   gint run;
  117. } WarpInterface;
  118.  
  119. /*
  120.  * Function prototypes.
  121.  */
  122.  
  123. static void      query  (void);
  124. static void      run    (gchar    *name,
  125.              gint      nparams,
  126.              GimpParam   *param,
  127.              gint     *nreturn_vals,
  128.              GimpParam  **return_vals);
  129.  
  130. static void      blur16           (GimpDrawable *drawable);
  131.  
  132. static void      diff             (GimpDrawable *drawable, 
  133.                    gint32    *xl_id, 
  134.                    gint32    *yl_id);
  135.  
  136. static void      diff_prepare_row (GimpPixelRgn  *pixel_rgn,
  137.                    guchar     *data,
  138.                    int         x,
  139.                    int         y,
  140.                    int         w);
  141.  
  142. static void      warp_one         (GimpDrawable *draw, 
  143.                    GimpDrawable *new,
  144.                    GimpDrawable *map_x, 
  145.                    GimpDrawable *map_y,
  146.                    GimpDrawable *mag_draw,
  147.                    gint       first_time,
  148.                    gint       step);
  149.  
  150. static void      warp        (GimpDrawable  *drawable,
  151.                   GimpDrawable **map_x_p,
  152.                   GimpDrawable **map_y_p);
  153.  
  154. static gint      warp_dialog (GimpDrawable *drawable);
  155. static GimpTile *   warp_pixel  (GimpDrawable *drawable,
  156.                   GimpTile     *tile,
  157.                   gint       width,
  158.                   gint       height,
  159.                   gint       x1,
  160.                   gint       y1,
  161.                   gint       x2,
  162.                   gint       y2,
  163.                   gint       x,
  164.                   gint       y,
  165.                   gint      *row,
  166.                   gint      *col,
  167.                   guchar    *pixel);
  168.  
  169. static guchar    bilinear        (gdouble  x,
  170.                   gdouble  y,
  171.                   guchar  *v);
  172.  
  173. static gint      bilinear16      (gdouble  x,
  174.                   gdouble  y,
  175.                   gint    *v);
  176.  
  177. static gint      warp_map_constrain       (gint32     image_id,
  178.                        gint32     drawable_id,
  179.                        gpointer   data);
  180. static void      warp_map_callback        (gint32     id,
  181.                        gpointer   data);
  182. static void      warp_map_mag_callback    (gint32     id,
  183.                        gpointer   data);
  184. static void      warp_map_grad_callback   (gint32     id,
  185.                        gpointer   data);
  186. static void      warp_map_vector_callback (gint32     id,
  187.                        gpointer   data);
  188. static void      warp_ok_callback         (GtkWidget *widget,
  189.                        gpointer   data);
  190.  
  191. static gdouble   warp_map_mag_give_value  (guchar    *pt, 
  192.                        gint       alpha, 
  193.                        gint       bytes);
  194.  
  195. /* -------------------------------------------------------------------------- */
  196. /*   Variables global over entire plug-in scope                               */
  197. /* -------------------------------------------------------------------------- */
  198.  
  199. GimpPlugInInfo PLUG_IN_INFO =
  200. {
  201.   NULL,  /* init_proc  */
  202.   NULL,  /* quit_proc  */
  203.   query, /* query_proc */
  204.   run,   /* run_proc   */
  205. };
  206.  
  207. static WarpVals dvals =
  208. {
  209.   10.0,   /* amount       */
  210.   -1,     /* warp_map     */
  211.   5,      /* iterations   */
  212.   0.0,    /* dither       */
  213.   90.0,   /* angle        */
  214.   WRAP,   /* wrap_type    */
  215.   -1,     /* mag_map      */
  216.   FALSE,  /* mag_use      */
  217.   1,      /* substeps     */
  218.   -1,     /* grad_map     */
  219.   0.0,    /* grad_scale   */
  220.   -1,     /* vector_map   */
  221.   0.0,    /* vector_scale */
  222.   0.0     /* vector_angle */
  223. };
  224.  
  225. static WarpInterface dint =
  226. {
  227.   FALSE,  /* run */
  228. };
  229.  
  230. /* -------------------------------------------------------------------------- */
  231.  
  232. /* static gint         display_diff_map = TRUE;   show 16-bit diff. vectormap */
  233. static gint         progress = 0;              /* progress indicator bar      */
  234. static guint        tile_width, tile_height;   /* size of an image tile       */
  235. static GimpRunModeType run_mode;                  /* interactive, non-, etc.     */
  236. static guchar       color_pixel[4] = {0, 0, 0, 255};  /* current fg color     */
  237.  
  238. /* -------------------------------------------------------------------------- */
  239.  
  240. /***** Functions *****/
  241.  
  242. MAIN ()
  243.  
  244. static void
  245. query (void)
  246. {
  247.   static GimpParamDef args[] =
  248.   {
  249.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  250.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  251.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  252.     { GIMP_PDB_FLOAT, "amount", "Pixel displacement multiplier" },
  253.     { GIMP_PDB_DRAWABLE, "warp_map", "Displacement control map" },
  254.     { GIMP_PDB_INT32, "iter", "Iteration count (last required argument)" },
  255.     { GIMP_PDB_FLOAT, "dither", "Random dither amount (first optional argument)" },
  256.     { GIMP_PDB_FLOAT, "angle", "Angle of gradient vector rotation" },
  257.     { GIMP_PDB_INT32, "wrap_type", "Edge behavior: { WRAP (0), SMEAR (1), BLACK (2), COLOR (3) }" },
  258.     { GIMP_PDB_DRAWABLE, "mag_map", "Magnitude control map" },
  259.     { GIMP_PDB_INT32, "mag_use", "Use magnitude map: { FALSE (0), TRUE (1) }" },
  260.     { GIMP_PDB_INT32, "substeps", "Substeps between image updates" },
  261.     { GIMP_PDB_INT32, "grad_map", "Gradient control map" },
  262.     { GIMP_PDB_FLOAT, "grad_scale", "Scaling factor for gradient map (0=don't use)" },
  263.     { GIMP_PDB_INT32, "vector_map", "Fixed vector control map" },
  264.     { GIMP_PDB_FLOAT, "vector_scale", "Scaling factor for fixed vector map (0=don't use)" },
  265.     { GIMP_PDB_FLOAT, "vector_angle", "Angle for fixed vector map" }
  266.   };
  267.   static gint nargs = sizeof (args) / sizeof (args[0]);
  268.  
  269.   gimp_install_procedure ("plug_in_warp",
  270.               "Twist or smear an image. (only first six arguments are required)",
  271.               "Smears an image along vector paths calculated as "
  272.               "the gradient of a separate control matrix. The "
  273.               "effect can look like brushstrokes of acrylic or "
  274.               "watercolor paint, in some cases.",
  275.               "John P. Beale",
  276.               "John P. Beale",
  277.               "1997",
  278.               N_("<Image>/Filters/Map/Warp..."),
  279.               "RGB*, GRAY*",
  280.               GIMP_PLUGIN,
  281.               nargs, 0,
  282.               args, NULL);
  283. }
  284.  
  285. static void
  286. run (gchar  *name,
  287.      gint    nparams,
  288.      GimpParam  *param,
  289.      gint   *nreturn_vals,
  290.      GimpParam **return_vals)
  291. {
  292.   static GimpParam values[1];
  293.   GimpDrawable *drawable;
  294.   GimpDrawable *map_x = NULL;   /* satisfy compiler complaints */
  295.   GimpDrawable *map_y = NULL;
  296.   gint32 image_ID;           /* image id of drawable */
  297.  
  298.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  299.   gint pcnt;                 /* parameter counter for scanning input params. */
  300.  
  301.   run_mode = param[0].data.d_int32;
  302.  
  303.   tile_width = gimp_tile_width();    /* initialize some globals */
  304.   tile_height = gimp_tile_height();
  305.  
  306.   /* get currently selected foreground pixel color */
  307.   gimp_palette_get_foreground (&color_pixel[0],
  308.                    &color_pixel[1],
  309.                    &color_pixel[2]);
  310.  
  311.   /*  Get the specified drawable  */
  312.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  313.  
  314.   *nreturn_vals = 1;
  315.   *return_vals  = values;
  316.   values[0].type          = GIMP_PDB_STATUS;
  317.   values[0].data.d_status = status;
  318.  
  319.   switch (run_mode)
  320.     {
  321.     case GIMP_RUN_INTERACTIVE:
  322.       INIT_I18N_UI();
  323.       /*  Possibly retrieve data  */
  324.       gimp_get_data ("plug_in_warp", &dvals);
  325.  
  326.       /*  First acquire information with a dialog  */
  327.       if (! warp_dialog (drawable))
  328.     return;
  329.       break;
  330.  
  331.     case GIMP_RUN_NONINTERACTIVE:
  332.       INIT_I18N();
  333.       /*  Make sure minimum args
  334.        *  (mode, image, draw, amount, warp_map, iter) are there 
  335.        */
  336.       if (nparams < MIN_ARGS)
  337.     {
  338.       status = GIMP_PDB_CALLING_ERROR;
  339.     }
  340.       else
  341.     {
  342.       pcnt = MIN_ARGS;                          /* parameter counter */
  343.       dvals.amount   = param[3].data.d_float;
  344.       dvals.warp_map = param[4].data.d_int32;
  345.       dvals.iter     = param[5].data.d_int32;
  346.       if (nparams > pcnt++) dvals.dither       = param[6].data.d_float;
  347.       if (nparams > pcnt++) dvals.angle        = param[7].data.d_float;
  348.       if (nparams > pcnt++) dvals.wrap_type    = param[8].data.d_int32;
  349.       if (nparams > pcnt++) dvals.mag_map      = param[9].data.d_int32;
  350.       if (nparams > pcnt++) dvals.mag_use      = param[10].data.d_int32;
  351.       if (nparams > pcnt++) dvals.substeps     = param[11].data.d_int32;
  352.       if (nparams > pcnt++) dvals.grad_map     = param[12].data.d_int32;
  353.       if (nparams > pcnt++) dvals.grad_scale   = param[13].data.d_float;
  354.       if (nparams > pcnt++) dvals.vector_map   = param[14].data.d_int32;
  355.       if (nparams > pcnt++) dvals.vector_scale = param[15].data.d_float;
  356.       if (nparams > pcnt++) dvals.vector_angle = param[16].data.d_float;
  357.     }
  358.       break;
  359.  
  360.     case GIMP_RUN_WITH_LAST_VALS:
  361.       /*  Possibly retrieve data  */
  362.       gimp_get_data ("plug_in_warp", &dvals);
  363.       break;
  364.  
  365.     default:
  366.       break;
  367.     }
  368.  
  369.   if (status == GIMP_PDB_SUCCESS)
  370.     {
  371.       /*  set the tile cache size  */
  372.       gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
  373.  
  374.       /*  run the warp effect  */
  375.       warp (drawable, &map_x, &map_y);
  376.  
  377.       /*  Store data  */
  378.       if (run_mode == GIMP_RUN_INTERACTIVE)
  379.     gimp_set_data ("plug_in_warp", &dvals, sizeof (WarpVals));
  380.     }
  381.  
  382.   values[0].data.d_status = status;
  383.  
  384.   image_ID = gimp_layer_get_image_id (map_x->id);
  385.  
  386.   gimp_drawable_detach (map_x);
  387.   gimp_drawable_detach (map_y);
  388.  
  389.   gimp_image_delete (image_ID);
  390.  
  391.   if (run_mode != GIMP_RUN_NONINTERACTIVE)
  392.     gimp_displays_flush ();
  393. }
  394.  
  395. static int
  396. warp_dialog (GimpDrawable *drawable)
  397. {
  398.   GtkWidget *dlg;
  399.   GtkWidget *vbox;
  400.   GtkWidget *label;
  401.   GtkWidget *toggle;
  402.   GtkWidget *toggle_hbox;
  403.   GtkWidget *frame;
  404.   GtkWidget *table;
  405.   GtkWidget *otable;
  406.   GtkWidget *spinbutton;
  407.   GtkObject *adj;
  408.   GtkWidget *option_menu;
  409.   GtkWidget *option_menu_mag;
  410.   GtkWidget *option_menu_grad;
  411.   GtkWidget *option_menu_vector;
  412.   GtkWidget *menu;
  413.   GtkWidget *magmenu;
  414.   GtkWidget *gradmenu;
  415.   GtkWidget *vectormenu;
  416.  
  417.   GSList  *group = NULL;
  418.  
  419.   gimp_ui_init ("warp", FALSE);
  420.  
  421.   dlg = gimp_dialog_new (_("Warp"), "warp",
  422.              gimp_standard_help_func, "filters/warp.html",
  423.              GTK_WIN_POS_MOUSE,
  424.              FALSE, TRUE, FALSE,
  425.  
  426.              _("OK"), warp_ok_callback,
  427.              NULL, NULL, NULL, TRUE, FALSE,
  428.              _("Cancel"), gtk_widget_destroy,
  429.              NULL, 1, NULL, FALSE, TRUE,
  430.  
  431.              NULL);
  432.  
  433.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  434.               GTK_SIGNAL_FUNC (gtk_main_quit),
  435.               NULL);
  436.  
  437.   gimp_help_init ();
  438.  
  439.   vbox = gtk_vbox_new (FALSE, 4);
  440.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  441.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
  442.   gtk_widget_show (vbox);
  443.  
  444.   frame = gtk_frame_new (_("Main Options"));
  445.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  446.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  447.  
  448.   table = gtk_table_new (3, 3, FALSE);
  449.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  450.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  451.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  452.   gtk_container_add (GTK_CONTAINER (frame), table);
  453.  
  454.   /*  amount, iter */
  455.   spinbutton = gimp_spin_button_new (&adj, dvals.amount,
  456.                      -1000, 1000, /* ??? */
  457.                      1, 10, 0, 1, 2);
  458.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  459.                  _("Step Size:"), 1.0, 0.5,
  460.                  spinbutton, 1, TRUE);
  461.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  462.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  463.               &dvals.amount);
  464.  
  465.   spinbutton = gimp_spin_button_new (&adj, dvals.iter,
  466.                      1, 100, 1, 5, 0, 1, 0);
  467.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  468.                  _("Iterations:"), 1.0, 0.5,
  469.                  spinbutton, 1, TRUE);
  470.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  471.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  472.               &dvals.iter);
  473.  
  474.   /*  Displacement map menu  */
  475.   label = gtk_label_new (_("Displacement Map:"));
  476.   gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
  477.             GTK_FILL, GTK_FILL, 0, 0);
  478.   gtk_widget_show (label);
  479.  
  480.   option_menu = gtk_option_menu_new ();
  481.   gtk_table_attach (GTK_TABLE (table), option_menu, 2, 3, 1, 2,
  482.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  483.   menu = gimp_drawable_menu_new (warp_map_constrain, warp_map_callback,
  484.                  drawable, dvals.warp_map);
  485.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
  486.   gtk_widget_show (option_menu);
  487.  
  488.   /* ======================================================================= */
  489.  
  490.   /*  Displacement Type  */
  491.   label = gtk_label_new (_("On Edges:"));
  492.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  493.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
  494.             GTK_FILL, GTK_FILL, 0, 0);
  495.   gtk_widget_show (label);
  496.  
  497.   toggle_hbox = gtk_hbox_new (FALSE, 4);
  498.   gtk_table_attach (GTK_TABLE (table), toggle_hbox, 1, 3, 2, 3,
  499.             GTK_FILL, GTK_FILL, 0, 0);
  500.  
  501.   toggle = gtk_radio_button_new_with_label (group, _("Wrap"));
  502.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  503.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  504.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) WRAP);
  505.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  506.               (GtkSignalFunc) gimp_radio_button_update,
  507.               &dvals.wrap_type);
  508.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  509.                 dvals.wrap_type == WRAP);
  510.   gtk_widget_show (toggle);
  511.  
  512.   toggle = gtk_radio_button_new_with_label (group, _("Smear"));
  513.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  514.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  515.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) SMEAR);
  516.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  517.               (GtkSignalFunc) gimp_radio_button_update,
  518.               &dvals.wrap_type);
  519.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  520.                 dvals.wrap_type == SMEAR);
  521.   gtk_widget_show (toggle);
  522.  
  523.   toggle = gtk_radio_button_new_with_label (group, _("Black"));
  524.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  525.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  526.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) BLACK);
  527.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  528.               (GtkSignalFunc) gimp_radio_button_update,
  529.               &dvals.wrap_type);
  530.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  531.                 dvals.wrap_type == BLACK);
  532.   gtk_widget_show (toggle);
  533.  
  534.   toggle = gtk_radio_button_new_with_label (group, _("FG Color"));
  535.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  536.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  537.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) COLOR);
  538.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  539.               (GtkSignalFunc) gimp_radio_button_update,
  540.               &dvals.wrap_type);
  541.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  542.                 dvals.wrap_type == COLOR);
  543.   gtk_widget_show (toggle);
  544.  
  545.   gtk_widget_show (toggle_hbox);
  546.  
  547.   gtk_widget_show (table);
  548.   gtk_widget_show (frame);
  549.  
  550.   /* -------------------------------------------------------------------- */
  551.   /* ---------    The secondary table         --------------------------  */
  552.  
  553.   frame = gtk_frame_new (_("Secondary Options"));
  554.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  555.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  556.  
  557.   table = gtk_table_new (3, 3, FALSE);
  558.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  559.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  560.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  561.   gtk_container_add (GTK_CONTAINER (frame), table);
  562.  
  563.   spinbutton = gimp_spin_button_new (&adj, dvals.dither,
  564.                      0, 100, 1, 10, 0, 1, 2);
  565.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  566.                  _("Dither Size:"), 1.0, 0.5,
  567.                  spinbutton, 1, TRUE);
  568.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  569.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  570.               &dvals.dither);
  571.  
  572.   spinbutton = gimp_spin_button_new (&adj, dvals.angle,
  573.                      0, 360, 1, 15, 0, 1, 1);
  574.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  575.                  _("Rotation Angle:"), 1.0, 0.5,
  576.                  spinbutton, 1, TRUE);
  577.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  578.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  579.               &dvals.angle);
  580.  
  581.   spinbutton = gimp_spin_button_new (&adj, dvals.substeps,
  582.                      1, 100, 1, 5, 0, 1, 0);
  583.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
  584.                  _("Substeps:"), 1.0, 0.5,
  585.                  spinbutton, 1, TRUE);
  586.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  587.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  588.               &dvals.substeps);
  589.  
  590.   /*  Magnitude map menu  */
  591.   label = gtk_label_new (_("Magnitude Map:"));
  592.   gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
  593.             GTK_FILL, GTK_FILL, 0, 0);
  594.   gtk_widget_show (label);
  595.  
  596.   option_menu_mag = gtk_option_menu_new ();
  597.   gtk_table_attach (GTK_TABLE (table), option_menu_mag, 2, 3, 1, 2,
  598.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  599.   magmenu = gimp_drawable_menu_new (warp_map_constrain, warp_map_mag_callback,
  600.                     drawable, dvals.mag_map);
  601.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu_mag), magmenu);
  602.   gtk_widget_show (option_menu_mag);
  603.  
  604.   /*  Magnitude Usage  */
  605.   toggle_hbox = gtk_hbox_new (FALSE, 4);
  606.   gtk_container_set_border_width (GTK_CONTAINER (toggle_hbox), 1);
  607.   gtk_table_attach (GTK_TABLE (table), toggle_hbox, 2, 3, 2, 3,
  608.             GTK_FILL, GTK_FILL, 0, 0);
  609.  
  610.   toggle = gtk_check_button_new_with_label (_("Use Mag Map"));
  611.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  612.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  613.               (GtkSignalFunc) gimp_toggle_button_update,
  614.               &dvals.mag_use);
  615.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), dvals.mag_use);
  616.   gtk_widget_show (toggle);
  617.  
  618.   gtk_widget_show (toggle_hbox);
  619.  
  620.   gtk_widget_show (table);
  621.   gtk_widget_show (frame);
  622.  
  623.   /* -------------------------------------------------------------------- */
  624.   /* ---------    The "other" table         --------------------------  */
  625.  
  626.   frame = gtk_frame_new (_("Other Options"));
  627.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  628.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  629.  
  630.   otable = gtk_table_new (3, 3, FALSE);
  631.   gtk_table_set_row_spacings (GTK_TABLE (otable), 2);
  632.   gtk_table_set_col_spacings (GTK_TABLE (otable), 4);
  633.   gtk_container_set_border_width (GTK_CONTAINER (otable), 4);
  634.   gtk_container_add (GTK_CONTAINER (frame), otable);
  635.  
  636.   spinbutton = gimp_spin_button_new (&adj, dvals.grad_scale,
  637.                      -1000, 1000, /* ??? */
  638.                      0.01, 0.1, 0, 1, 3);
  639.   gimp_table_attach_aligned (GTK_TABLE (otable), 0, 0,
  640.                  _("Gradient Scale:"), 1.0, 0.5,
  641.                  spinbutton, 1, TRUE);
  642.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  643.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  644.               &dvals.grad_scale);
  645.  
  646.   /* ---------  Gradient map menu ----------------  */
  647.  
  648.   option_menu_grad = gtk_option_menu_new ();
  649.   gtk_table_attach (GTK_TABLE (otable), option_menu_grad, 2, 3, 0, 1,
  650.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  651.   gradmenu = gimp_drawable_menu_new (warp_map_constrain, warp_map_grad_callback,
  652.                  drawable, dvals.grad_map);
  653.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu_grad), gradmenu);
  654.   gimp_help_set_help_data (option_menu_grad,
  655.                _("Gradient map selection menu"), NULL);
  656.  
  657.   gtk_widget_show (option_menu_grad);
  658.  
  659.   /* ---------------------------------------------- */
  660.  
  661.   spinbutton = gimp_spin_button_new (&adj, dvals.vector_scale,
  662.                      -1000, 1000, /* ??? */
  663.                      0.01, 0.1, 0, 1, 3);
  664.   gimp_table_attach_aligned (GTK_TABLE (otable), 0, 1,
  665.                  _("Vector Mag:"), 1.0, 0.5,
  666.                  spinbutton, 1, TRUE);
  667.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  668.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  669.               &dvals.vector_scale);
  670.  
  671.   /* -------------------------------------------------------- */
  672.  
  673.   spinbutton = gimp_spin_button_new (&adj, dvals.vector_angle,
  674.                      0, 360, 1, 15, 0, 1, 1);
  675.   gimp_table_attach_aligned (GTK_TABLE (otable), 0, 2,
  676.                  _("Angle:"), 1.0, 0.5,
  677.                  spinbutton, 1, TRUE);
  678.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  679.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  680.               &dvals.vector_angle);
  681.  
  682.   /* ---------  Vector map menu ----------------  */
  683.   option_menu_vector = gtk_option_menu_new ();
  684.   gtk_table_attach (GTK_TABLE (otable), option_menu_vector, 2, 3, 1, 2,
  685.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  686.   vectormenu = gimp_drawable_menu_new (warp_map_constrain,
  687.                        warp_map_vector_callback,
  688.                        drawable, dvals.vector_map);
  689.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu_vector), vectormenu);
  690.   gimp_help_set_help_data (option_menu_vector,
  691.                _("Fixed-direction-vector map selection menu"),
  692.                NULL);
  693.  
  694.   gtk_widget_show (option_menu_vector);
  695.  
  696.   gtk_widget_show (otable);
  697.   gtk_widget_show (frame);
  698.  
  699.   gtk_widget_show (dlg);
  700.  
  701.   gtk_main ();
  702.   gimp_help_free ();
  703.   gdk_flush ();
  704.  
  705.   return dint.run;
  706. }
  707. /* ---------------------------------------------------------------------- */
  708.  
  709. static void
  710. blur16 (GimpDrawable *drawable)
  711. {
  712.   /*  blur a 2-or-more byte-per-pixel drawable,
  713.    *  1st 2 bytes interpreted as a 16-bit height field.
  714.    */
  715.   GimpPixelRgn srcPR, destPR;
  716.   gint width, height;
  717.   gint src_bytes;
  718.   gint dest_bytes;
  719.   gint dest_bytes_inc;
  720.   gint offb, off1;
  721.  
  722.   guchar *dest, *d;  /* pointers to rows of X and Y diff. data */
  723.   guchar *prev_row, *pr;
  724.   guchar *cur_row, *cr;
  725.   guchar *next_row, *nr;
  726.   guchar *tmp;
  727.   gint row, col;  /* relating to indexing into pixel row arrays */
  728.   gint x1, y1, x2, y2;
  729.   gdouble pval;          /* average pixel value of pixel & neighbors */
  730.  
  731.   /* --------------------------------------- */
  732.  
  733.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  734.  
  735.   width = drawable->width;     /* size of input drawable*/
  736.   height = drawable->height;
  737.   src_bytes = drawable->bpp;   /* bytes per pixel in SOURCE drawable, must be 2 or more  */
  738.   dest_bytes = drawable->bpp;   /* bytes per pixel in SOURCE drawable, >= 2  */
  739.   dest_bytes_inc = dest_bytes - 2;  /* this is most likely zero, but I guess it's more conservative... */
  740.  
  741.   /*  allocate row buffers for source & dest. data  */
  742.  
  743.   prev_row = (guchar *) malloc ((x2 - x1 + 2) * src_bytes);
  744.   cur_row = (guchar *) malloc ((x2 - x1 + 2) * src_bytes);
  745.   next_row = (guchar *) malloc ((x2 - x1 + 2) * src_bytes);
  746.   dest = (guchar *) malloc ((x2 - x1) * src_bytes);
  747.  
  748.   /* initialize the pixel regions (read from source, write into dest)  */
  749.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  750.   gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
  751.  
  752.   pr = prev_row + src_bytes;   /* row arrays are prepared for indexing to -1 (!) */
  753.   cr = cur_row + src_bytes;
  754.   nr = next_row + src_bytes;
  755.  
  756.   diff_prepare_row (&srcPR, pr, x1, y1, (x2 - x1));
  757.   diff_prepare_row (&srcPR, cr, x1, y1+1, (x2 - x1));
  758.  
  759.   /*  loop through the rows, applying the smoothing function  */
  760.   for (row = y1; row < y2; row++)
  761.     {
  762.       /*  prepare the next row  */
  763.       diff_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));
  764.  
  765.       d = dest;
  766.       for (col = 0; col < (x2 - x1); col++) /* over columns of pixels */
  767.     {
  768.       offb = col*src_bytes;    /* base of byte pointer offset */
  769.       off1 = offb+1;                 /* offset into row arrays */
  770.  
  771.       pval = (256.0*pr[offb - src_bytes] + pr[off1 - src_bytes] +
  772.           256.0*pr[offb] + pr[off1] +
  773.           256.0*pr[offb + src_bytes] + pr[off1 + src_bytes] +
  774.           256.0*cr[offb - src_bytes] + cr[off1 - src_bytes] +
  775.           256.0*cr[offb]  + cr[off1] +
  776.           256.0*cr[offb + src_bytes] + cr[off1 + src_bytes] +
  777.           256.0*nr[offb - src_bytes] + nr[off1 - src_bytes] +
  778.           256.0*nr[offb] + nr[off1] +
  779.           256.0*nr[offb + src_bytes]) + nr[off1 + src_bytes];
  780.  
  781.       pval /= 9.0;  /* take the average */
  782.       *d++ = (guchar) (((gint)pval)>>8);   /* high-order byte */
  783.       *d++ = (guchar) (((gint)pval)%256);  /* low-order byte */
  784.       d += dest_bytes_inc;       /* move data pointer on to next destination pixel */
  785.          
  786.     }
  787.       /*  store the dest  */
  788.       gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2 - x1));
  789.  
  790.       /*  shuffle the row pointers  */
  791.       tmp = pr;
  792.       pr = cr;
  793.       cr = nr;
  794.       nr = tmp;  
  795.  
  796.       if ((row % 5) == 0)
  797.     gimp_progress_update ((double) row / (double) (y2 - y1));
  798.     }
  799.  
  800.   /*  update the region  */
  801.   gimp_drawable_flush (drawable);
  802.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  803.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  804.  
  805.   free (prev_row);  /* row buffers allocated at top of fn. */
  806.   free (cur_row);
  807.   free (next_row);
  808.   free (dest);
  809.  
  810. } /* end blur16() */
  811.  
  812.  
  813. /* ====================================================================== */
  814. /* Get one row of pixels from the PixelRegion and put them in 'data'      */
  815.  
  816. static void
  817. diff_prepare_row (GimpPixelRgn *pixel_rgn,
  818.           guchar    *data,
  819.           int        x,
  820.           int        y,
  821.           int        w)
  822. {
  823.   int b;
  824.  
  825.   if (y == 0)    /* wrap around */
  826.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, (pixel_rgn->h - 1), w);
  827.   else if (y == pixel_rgn->h)
  828.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, 1, w);
  829.   else
  830.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, y, w);
  831.  
  832.   /*  Fill in edge pixels  */
  833.   for (b = 0; b < pixel_rgn->bpp; b++)
  834.     {
  835.       data[-pixel_rgn->bpp + b] = data[b];
  836.       data[w * pixel_rgn->bpp + b] = data[(w - 1) * pixel_rgn->bpp + b];
  837.     }
  838. }
  839.  
  840. /* -------------------------------------------------------------------------- */
  841. /*  'diff' combines the input drawables to prepare the two                    */
  842. /*  16-bit (X,Y) vector displacement maps                                     */
  843. /* -------------------------------------------------------------------------- */
  844.  
  845. static void
  846. diff (GimpDrawable *drawable, 
  847.       gint32    *xl_id, 
  848.       gint32    *yl_id)
  849. {
  850.   GimpDrawable *draw_xd, *draw_yd;  /* vector disp. drawables */
  851.   GimpDrawable *mdraw, *vdraw, *gdraw;
  852.   gint32 image_id;           /* image holding X and Y diff. arrays */
  853.   gint32 new_image_id;       /* image holding X and Y diff. layers */
  854.   gint32 layer_active;       /* currently active layer */
  855.   gint32 xlayer_id, ylayer_id;   /* individual X and Y layer ID numbers */
  856.   GimpPixelRgn srcPR, destxPR, destyPR;
  857.   GimpPixelRgn vecPR, magPR, gradPR;
  858.   gint width, height;
  859.   gint src_bytes;
  860.   gint mbytes=0;
  861.   gint vbytes=0;
  862.   gint gbytes=0;   /* bytes-per-pixel of various source drawables */
  863.   gint dest_bytes;
  864.   gint dest_bytes_inc;
  865.   gint do_gradmap = FALSE;          /* whether to add in gradient of gradmap to final diff. map */
  866.   gint do_vecmap = FALSE;          /* whether to add in a fixed vector scaled by the vector map */
  867.   gint do_magmap = FALSE;          /* whether to multiply result by the magnitude map */
  868.  
  869.   guchar *destx, *dx, *desty, *dy;  /* pointers to rows of X and Y diff. data */
  870.   guchar *tmp;
  871.   guchar *prev_row, *pr;
  872.   guchar *cur_row, *cr;
  873.   guchar *next_row, *nr;
  874.   guchar *prev_row_g, *prg=NULL;          /* pointers to gradient map data */
  875.   guchar *cur_row_g, *crg=NULL;
  876.   guchar *next_row_g, *nrg=NULL;
  877.   guchar *cur_row_v, *crv=NULL;      /* pointers to vector map data */
  878.   guchar *cur_row_m, *crm=NULL;      /* pointers to magnitude map data */
  879.   gint row, col, offb, off, bytes;   /* relating to indexing into pixel row arrays */
  880.   gint x1, y1, x2, y2;
  881.   gint dvalx, dvaly;                  /* differential value at particular pixel */
  882.   gdouble tx, ty;                     /* temporary x,y differential value increments from gradmap, etc. */
  883.   gdouble rdx, rdy;                     /* x,y differential values: real #s */
  884.   gdouble rscalefac;                  /* scaling factor for x,y differential of 'curl' map */
  885.   gdouble gscalefac;                  /* scaling factor for x,y differential of 'gradient' map */
  886.   gdouble r, theta, dtheta;           /* rectangular<-> spherical coordinate transform for vector rotation */
  887.   gdouble scale_vec_x, scale_vec_y;   /* fixed vector X,Y component scaling factors */
  888.   gint has_alpha, ind;
  889.   
  890.   /* ----------------------------------------------------------------------- */
  891.  
  892.   if (dvals.grad_scale != 0.0)
  893.       do_gradmap = TRUE;    /* add in gradient of gradmap if scale != 0.000 */
  894.   if (dvals.vector_scale != 0.0)   /* add in gradient of vectormap if scale != 0.000 */
  895.       do_vecmap = TRUE;
  896.   do_magmap = (dvals.mag_use == TRUE);   /* multiply by magnitude map if so requested */
  897.  
  898.   /* Get the input area. This is the bounding box of the selection in
  899.    *  the image (or the entire image if there is no selection). Only
  900.    *  operating on the input area is simply an optimization. It doesn't
  901.    *  need to be done for correct operation. (It simply makes it go
  902.    *  faster, since fewer pixels need to be operated on).
  903.    */
  904.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  905.  
  906.   /* Get the size of the input image. (This will/must be the same
  907.    *  as the size of the output image.
  908.    */
  909.   width = drawable->width;
  910.   height = drawable->height;
  911.   src_bytes = drawable->bpp;                /* bytes per pixel in SOURCE drawable */
  912.   has_alpha = gimp_drawable_has_alpha(drawable->id);
  913.  
  914.   /* -- Add two layers: X and Y Displacement vectors -- */
  915.   /* -- I'm using a RGB  drawable and using the first two bytes for a 
  916.         16-bit pixel value. This is either clever, or a kluge, 
  917.         depending on your point of view.  */
  918.  
  919.   image_id = gimp_layer_get_image_id(drawable->id);
  920.   layer_active = gimp_image_get_active_layer(image_id);
  921.  
  922.   new_image_id = gimp_image_new(width, height, GIMP_RGB); /* create new image for X,Y diff */
  923.  
  924.   xlayer_id = gimp_layer_new(new_image_id, "Warp_X_Vectors",
  925.                  width, height,
  926.                  GIMP_RGB_IMAGE, 100.0, GIMP_NORMAL_MODE);
  927.  
  928.   ylayer_id = gimp_layer_new(new_image_id, "Warp_Y_Vectors",
  929.                  width, height,
  930.                  GIMP_RGB_IMAGE, 100.0, GIMP_NORMAL_MODE);
  931.  
  932.   draw_yd = gimp_drawable_get (ylayer_id);
  933.   draw_xd = gimp_drawable_get (xlayer_id);
  934.  
  935.     gimp_image_add_layer (new_image_id, xlayer_id, 1);
  936.     gimp_image_add_layer (new_image_id, ylayer_id, 1);
  937.     gimp_drawable_fill(xlayer_id, GIMP_BG_IMAGE_FILL);
  938.     gimp_drawable_fill(ylayer_id, GIMP_BG_IMAGE_FILL);
  939.     gimp_image_set_active_layer(image_id, layer_active);
  940.  
  941.   dest_bytes = draw_xd->bpp;                /* bytes per pixel in destination drawable(s) */
  942.   /* for a GRAYA drawable, I would expect this to be two bytes; any more would be excess */
  943.   dest_bytes_inc = dest_bytes - 2;
  944.  
  945.  
  946.   /*  allocate row buffers for source & dest. data  */
  947.  
  948.   prev_row = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  949.   cur_row  = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  950.   next_row = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  951.  
  952.   prev_row_g = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  953.   cur_row_g  = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  954.   next_row_g = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  955.  
  956.   cur_row_v = g_new (guchar, (x2 - x1 + 2) * src_bytes);  /* vector map */
  957.   cur_row_m = g_new (guchar, (x2 - x1 + 2) * src_bytes);  /* magnitude map */
  958.  
  959.   destx = g_new (guchar, (x2 - x1) * dest_bytes);
  960.   desty = g_new (guchar, (x2 - x1) * dest_bytes);
  961.  
  962.   if ((desty==NULL) || (destx==NULL) || (cur_row_m==NULL) || (cur_row_v==NULL)
  963.     || (next_row_g==NULL) || (cur_row_g==NULL) || (prev_row_g==NULL)
  964.     || (next_row==NULL) || (cur_row==NULL) || (prev_row==NULL)) {
  965.    fprintf(stderr, "Warp diff: error allocating memory.\n");
  966.    exit(1);
  967.   }
  968.  
  969.   /*  initialize the source and destination pixel regions  */
  970.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);  /* 'curl' vector-rotation input */
  971.   gimp_pixel_rgn_init (&destxPR, draw_xd, 0, 0, width, height, TRUE, FALSE);  /* destination: X diff output */
  972.   gimp_pixel_rgn_init (&destyPR, draw_yd, 0, 0, width, height, TRUE, FALSE);  /* Y diff output */
  973.  
  974.   pr = prev_row + src_bytes;
  975.   cr = cur_row + src_bytes;
  976.   nr = next_row + src_bytes;
  977.  
  978.   diff_prepare_row (&srcPR, pr, x1, y1, (x2 - x1));
  979.   diff_prepare_row (&srcPR, cr, x1, y1+1, (x2 - x1));
  980.  
  981.  /* fixed-vector (x,y) component scale factors */
  982.   scale_vec_x = dvals.vector_scale*cos((90-dvals.vector_angle)*G_PI/180.0)*256.0/10;
  983.   scale_vec_y = dvals.vector_scale*sin((90-dvals.vector_angle)*G_PI/180.0)*256.0/10;
  984.  
  985.   if (do_vecmap) {
  986.     /*    fprintf(stderr,"%f %f  x,y vector components.\n",scale_vec_x,scale_vec_y); */
  987.  
  988.     vdraw = gimp_drawable_get(dvals.vector_map);
  989.     vbytes = vdraw->bpp;                /* bytes per pixel in SOURCE drawable */
  990.     gimp_pixel_rgn_init (&vecPR, vdraw, 0, 0, width, height, FALSE, FALSE);          /* fixed-vector scale-map */
  991.     crv = cur_row_v + vbytes;
  992.     diff_prepare_row (&vecPR, crv, x1, y1, (x2 - x1));
  993.   }
  994.   if (do_gradmap) {
  995.     gdraw = gimp_drawable_get(dvals.grad_map);
  996.     gbytes = gdraw->bpp;
  997.     gimp_pixel_rgn_init (&gradPR, gdraw, 0, 0, width, height, FALSE, FALSE);          /* fixed-vector scale-map */
  998.     prg = prev_row_g + gbytes;
  999.     crg = cur_row_g + gbytes;
  1000.     nrg = next_row_g + gbytes;
  1001.     diff_prepare_row (&gradPR, prg, x1, y1 - 1, (x2 - x1));
  1002.     diff_prepare_row (&gradPR, crg, x1, y1, (x2 - x1));
  1003.   }
  1004.   if (do_magmap) {
  1005.     mdraw = gimp_drawable_get(dvals.mag_map);
  1006.     mbytes = mdraw->bpp;
  1007.     gimp_pixel_rgn_init (&magPR, mdraw, 0, 0, width, height, FALSE, FALSE);          /* fixed-vector scale-map */
  1008.     crm = cur_row_m + mbytes;
  1009.     diff_prepare_row (&magPR, crm, x1, y1, (x2 - x1));
  1010.   }
  1011.  
  1012.   dtheta = dvals.angle * G_PI / 180.0;
  1013.   rscalefac = 256.0 / (3*src_bytes);         /* note that '3' is rather arbitrary here. */
  1014.   gscalefac = dvals.grad_scale* 256.0 / (3*gbytes);            /* scale factor for gradient map components */
  1015.  
  1016.   /*  loop through the rows, applying the differential convolution  */
  1017.   for (row = y1; row < y2; row++)
  1018.     {
  1019.       /*  prepare the next row  */
  1020.       diff_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));
  1021.   
  1022.       if (do_magmap)
  1023.         diff_prepare_row (&magPR, crm, x1, row + 1, (x2 - x1));
  1024.       if (do_vecmap)
  1025.         diff_prepare_row (&vecPR, crv, x1, row + 1, (x2 - x1));
  1026.       if (do_gradmap)
  1027.         diff_prepare_row (&gradPR, crg, x1, row + 1, (x2 - x1));
  1028.  
  1029.       dx = destx;
  1030.       dy = desty;
  1031.       ind = 0;
  1032.  
  1033.       for (col = 0; col < (x2 - x1); col++) /* over columns of pixels */
  1034.       {
  1035.          rdx = 0.0;
  1036.          rdy = 0.0;
  1037.          ty = 0.0;
  1038.          tx = 0.0;
  1039.  
  1040.          offb = col*src_bytes;    /* base of byte pointer offset */
  1041.          for (bytes=0; bytes < src_bytes; bytes++) /* add all channels together */
  1042.            {
  1043.            off = offb+bytes;                 /* offset into row arrays */
  1044.           rdx += ((gint) -pr[off - src_bytes]   + (gint) pr[off + src_bytes] +
  1045.               (gint) -2*cr[off - src_bytes] + (gint) 2*cr[off + src_bytes] +
  1046.               (gint) -nr[off - src_bytes]   + (gint) nr[off + src_bytes]);
  1047.  
  1048.            rdy += ((gint) -pr[off - src_bytes] - (gint)2*pr[off] - (gint) pr[off + src_bytes] +
  1049.               (gint) nr[off - src_bytes] + (gint)2*nr[off] + (gint) nr[off + src_bytes]);
  1050.            }
  1051.  
  1052.          rdx *= rscalefac;   /* take average, then reduce. Assume max. rdx now 65535 */
  1053.          rdy *= rscalefac;   /* take average, then reduce */     
  1054.  
  1055.          theta = atan2(rdy,rdx);          /* convert to polar, then back to rectang. coords */
  1056.          r = sqrt(rdy*rdy + rdx*rdx);
  1057.          theta += dtheta;              /* rotate gradient vector by this angle (radians) */
  1058.          rdx = r * cos(theta);
  1059.          rdy = r * sin(theta);
  1060.  
  1061.              if (do_gradmap) {
  1062.            
  1063.            offb = col*gbytes;     /* base of byte pointer offset into pixel values (R,G,B,Alpha, etc.) */
  1064.              for (bytes=0; bytes < src_bytes; bytes++) /* add all channels together */
  1065.             {
  1066.            off = offb+bytes;                 /* offset into row arrays */
  1067.           tx += ((gint) -prg[off - gbytes]   + (gint) prg[off + gbytes] +
  1068.               (gint) -2*crg[off - gbytes] + (gint) 2*crg[off + gbytes] +
  1069.               (gint) -nrg[off - gbytes]   + (gint) nrg[off + gbytes]);
  1070.  
  1071.            ty += ((gint) -prg[off - gbytes] - (gint)2*prg[off] - (gint) prg[off + gbytes] +
  1072.               (gint) nrg[off - gbytes] + (gint)2*nrg[off] + (gint) nrg[off + gbytes]);
  1073.             }
  1074.            tx *= gscalefac;
  1075.            ty *= gscalefac;
  1076.  
  1077.            rdx += tx;         /* add gradient component in to the other one */
  1078.            rdy += ty;
  1079.  
  1080.              } /* if (do_gradmap) */
  1081.          
  1082.  
  1083.          if (do_vecmap) {  /* add in fixed vector scaled by  vec. map data */
  1084.            tx = (gdouble) crv[col*vbytes];       /* use first byte only */
  1085.            rdx += scale_vec_x * tx;
  1086.            rdy += scale_vec_y * tx;
  1087.  
  1088.          } /* if (do_vecmap) */
  1089.  
  1090.          if (do_magmap) {  /* multiply result by mag. map data */
  1091.            tx = (gdouble) crm[col*mbytes];
  1092.            rdx = (rdx * tx)/(255.0);
  1093.            rdy = (rdy * tx)/(255.0);
  1094.  
  1095.          } /* if do_magmap */
  1096.  
  1097.  
  1098.          dvalx = rdx + (2<<14);         /* take zero point to be 2^15, since this is two bytes */
  1099.          dvaly = rdy + (2<<14);
  1100.  
  1101.              if (dvalx<0) dvalx=0;
  1102.          if (dvalx>65535) dvalx=65535;
  1103.          *dx++ = (guchar) (dvalx >> 8);    /* store high order byte in value channel */
  1104.              *dx++ = (guchar) (dvalx % 256);   /* store low order byte in alpha channel */
  1105.          dx += dest_bytes_inc;       /* move data pointer on to next destination pixel */
  1106.          
  1107.          if (dvaly<0) dvaly=0;
  1108.          if (dvaly>65535) dvaly=65535;
  1109.          *dy++ = (guchar) (dvaly >> 8);
  1110.              *dy++ = (guchar) (dvaly % 256);
  1111.          dy += dest_bytes_inc;
  1112.  
  1113.       } /* ------------------------------- for (col...) ----------------  */
  1114.  
  1115.       /*  store the dest  */
  1116.       gimp_pixel_rgn_set_row (&destxPR, destx, x1, row, (x2 - x1));
  1117.       gimp_pixel_rgn_set_row (&destyPR, desty, x1, row, (x2 - x1));
  1118.  
  1119.       /*  swap around the pointers to row buffers  */
  1120.       tmp = pr;
  1121.       pr = cr;
  1122.       cr = nr;
  1123.       nr = tmp;
  1124.  
  1125.       if (do_gradmap) {
  1126.         tmp = prg;
  1127.         prg = crg;
  1128.         crg = nrg;
  1129.         nrg = tmp;
  1130.       }
  1131.  
  1132.       if ((row % 5) == 0)
  1133.     gimp_progress_update ((double) row / (double) (y2 - y1));
  1134.  
  1135.     } /* for (row..) */
  1136.  
  1137.   /*  update the region  */
  1138.   gimp_drawable_flush (draw_xd);
  1139.   gimp_drawable_flush (draw_yd);
  1140.  
  1141.   gimp_drawable_update (draw_xd->id, x1, y1, (x2 - x1), (y2 - y1));
  1142.   gimp_drawable_update (draw_yd->id, x1, y1, (x2 - x1), (y2 - y1));
  1143.  
  1144.   /*
  1145.   if (display_diff_map) {
  1146.     gimp_display_new(new_image_id);
  1147.   }
  1148.   */
  1149.  
  1150.   gimp_displays_flush();  /* make sure layer is visible */
  1151.  
  1152.   gimp_progress_init ( _("Smoothing X gradient..."));
  1153.   blur16(draw_xd); 
  1154.   gimp_progress_init ( _("Smoothing Y gradient..."));
  1155.   blur16(draw_yd);
  1156.  
  1157.   g_free (prev_row);  /* row buffers allocated at top of fn. */
  1158.   g_free (cur_row);
  1159.   g_free (next_row);
  1160.   g_free (prev_row_g);  /* row buffers allocated at top of fn. */
  1161.   g_free (cur_row_g);
  1162.   g_free (next_row_g);
  1163.   g_free (cur_row_v);
  1164.   g_free (cur_row_m);
  1165.  
  1166.   g_free (destx);
  1167.   g_free (desty);
  1168.  
  1169.   *xl_id = xlayer_id;  /* pass back the X and Y layer ID numbers */
  1170.   *yl_id = ylayer_id;
  1171.  
  1172. } /* end diff() */
  1173.  
  1174. /* -------------------------------------------------------------------------- */
  1175. /*            The Warp displacement is done here.                             */
  1176. /* -------------------------------------------------------------------------- */
  1177.  
  1178. static void      
  1179. warp (GimpDrawable  *orig_draw,
  1180.       GimpDrawable **map_x,
  1181.       GimpDrawable **map_y)
  1182. {
  1183.   GimpDrawable *disp_map;    /* Displacement map, ie, control array */
  1184.   GimpDrawable *mag_draw;    /* Magnitude multiplier factor map */
  1185.  
  1186.   gchar *string;          /* string to hold title of progress bar window */
  1187.  
  1188.   gint    first_time = TRUE;
  1189.   gint    width;
  1190.   gint    height;
  1191.   gint    bytes;
  1192.   gint orig_image_id;
  1193.   gint image_type;
  1194.  
  1195.  
  1196.   gint    x1, y1, x2, y2;
  1197.  
  1198.   gint32 xdlayer = -1;
  1199.   gint32 ydlayer = -1;
  1200.  
  1201.   gint warp_iter;      /* index var. over all "warp" Displacement iterations */
  1202.  
  1203.  
  1204.   disp_map = gimp_drawable_get(dvals.warp_map);
  1205.   mag_draw = gimp_drawable_get(dvals.mag_map);
  1206.  
  1207.   /* calculate new X,Y Displacement image maps */
  1208.  
  1209.   gimp_progress_init ( _("Finding XY gradient..."));
  1210.  
  1211.   diff(disp_map, &xdlayer, &ydlayer);    /* generate x,y differential images (arrays) */
  1212.  
  1213.   /* Get selection area */
  1214.    gimp_drawable_mask_bounds (orig_draw->id, &x1, &y1, &x2, &y2);
  1215.  
  1216.    width  = orig_draw->width;
  1217.    height = orig_draw->height;
  1218.    bytes  = orig_draw->bpp;
  1219.    image_type = gimp_drawable_type(orig_draw->id);
  1220.  
  1221.    *map_x = gimp_drawable_get(xdlayer);
  1222.    *map_y = gimp_drawable_get(ydlayer);
  1223.  
  1224.    orig_image_id = gimp_layer_get_image_id(orig_draw->id);
  1225.  
  1226.    /*   gimp_image_lower_layer(orig_image_id, new_layer_id); */ /* hide it! */
  1227.  
  1228.    /*   gimp_layer_set_opacity(new_layer_id, 0.0); */
  1229.  
  1230.    for (warp_iter = 0; warp_iter < dvals.iter; warp_iter++)
  1231.    {
  1232.      if (run_mode != GIMP_RUN_NONINTERACTIVE) {
  1233.        string = g_strdup_printf (_("Flow Step %d..."), warp_iter+1);
  1234.        gimp_progress_init (string);
  1235.        g_free (string);
  1236.        progress = 0;
  1237.        gimp_progress_update (0);
  1238.      }
  1239.      warp_one(orig_draw, orig_draw, *map_x, *map_y, mag_draw, first_time, warp_iter);
  1240.  
  1241.      gimp_drawable_update (orig_draw->id, x1, y1, (x2 - x1), (y2 - y1));
  1242.  
  1243.      if (run_mode != GIMP_RUN_NONINTERACTIVE)
  1244.        gimp_displays_flush();
  1245.  
  1246.  
  1247.      first_time = FALSE;
  1248.  
  1249.    } /*  end for (warp_iter) */
  1250.  
  1251.    /* gimp_image_add_layer (orig_image_id, new_layer_id, 1); */  /* make layer visible in 'layers' dialog */
  1252.  
  1253. } /* Warp */
  1254.  
  1255. /* -------------------------------------------------------------------------- */
  1256.  
  1257. static void
  1258. warp_one (GimpDrawable *draw, 
  1259.       GimpDrawable *new,
  1260.       GimpDrawable *map_x, 
  1261.       GimpDrawable *map_y,
  1262.       GimpDrawable *mag_draw,
  1263.       gint       first_time,
  1264.       gint       step)
  1265. {
  1266.   GimpPixelRgn src_rgn;
  1267.   GimpPixelRgn dest_rgn;
  1268.   GimpPixelRgn map_x_rgn;
  1269.   GimpPixelRgn map_y_rgn;
  1270.   GimpPixelRgn mag_rgn;
  1271.   GimpTile   * tile = NULL;
  1272.   GimpTile   * xtile = NULL;
  1273.   GimpTile   * ytile = NULL;
  1274.   gint row=-1;
  1275.   gint xrow=-1;
  1276.   gint yrow=-1;
  1277.   gint col=-1;
  1278.   gint xcol=-1;
  1279.   gint ycol=-1;
  1280.  
  1281.   gpointer  pr;
  1282.  
  1283.   gint    width = -1;
  1284.   gint    height = -1;
  1285.   gint    dest_bytes=-1;
  1286.   gint    dmap_bytes=-1;
  1287.  
  1288.   guchar *destrow, *dest;
  1289.   guchar *srcrow, *src;
  1290.   guchar *mxrow=NULL, *mx;  /* NULL ptr. to make gcc's -Wall fn. happy */
  1291.   guchar *myrow=NULL, *my;
  1292.  
  1293.   guchar *mmagrow=NULL, *mmag=NULL;
  1294.   guchar  pixel[4][4];
  1295.   gint    x1, y1, x2, y2;
  1296.   gint    x, y;
  1297.   gint    max_progress;
  1298.  
  1299.   gdouble needx, needy;
  1300.   gdouble xval=0;      /* initialize to quiet compiler grumbles */
  1301.   gdouble yval=0;      /* interpolated vector displacement */
  1302.   gdouble scalefac;        /* multiplier for vector displacement scaling */
  1303.   gdouble dscalefac;       /* multiplier for incremental displacement vectors */
  1304.   gint    xi, yi;
  1305.   gint    substep;         /* loop variable counting displacement vector substeps */
  1306.  
  1307.   guchar  values[4];
  1308.   gint    ivalues[4];
  1309.   guchar  val;
  1310.  
  1311.   gint k;
  1312.  
  1313.   gdouble dx, dy;           /* X and Y Displacement, integer from GRAY map */
  1314.  
  1315.   gint    xm_alpha = 0;
  1316.   gint    ym_alpha = 0;
  1317.   gint    mmag_alpha = 0;
  1318.   gint    xm_bytes = 1;
  1319.   gint    ym_bytes = 1;
  1320.   gint    mmag_bytes = 1;
  1321.  
  1322.  
  1323. #ifdef G_OS_WIN32
  1324.   SRAND_FUNC (time(NULL));                   /* seed random # generator */
  1325. #else
  1326.   srand(time(NULL));                   /* seed random # generator */
  1327. #endif
  1328.  
  1329.   /* ================ Outer Loop calculation ================================ */
  1330.  
  1331.   /* Get selection area */
  1332.  
  1333.    gimp_drawable_mask_bounds (draw->id, &x1, &y1, &x2, &y2);
  1334.    width  = draw->width;
  1335.    height = draw->height;
  1336.    dest_bytes  = draw->bpp;
  1337.  
  1338.    dmap_bytes = map_x->bpp;
  1339.  
  1340.    max_progress = (x2 - x1) * (y2 - y1);
  1341.  
  1342.  
  1343.    /*  --------- Register the (many) pixel regions ----------  */
  1344.  
  1345.    gimp_pixel_rgn_init (&src_rgn, draw, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1346.  
  1347.    /* only push undo-stack the first time through. Thanks Spencer! */
  1348.    if (first_time==TRUE)
  1349.      gimp_pixel_rgn_init (&dest_rgn, new, x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
  1350.    else
  1351.      /*     gimp_pixel_rgn_init (&dest_rgn, new, x1, y1, (x2 - x1), (y2 - y1), TRUE, FALSE); */
  1352.      gimp_pixel_rgn_init (&dest_rgn, new, x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
  1353.  
  1354.  
  1355.    gimp_pixel_rgn_init (&map_x_rgn, map_x, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1356.    if (gimp_drawable_has_alpha(map_x->id))
  1357.     xm_alpha = 1;
  1358.    xm_bytes = gimp_drawable_bpp(map_x->id);
  1359.  
  1360.    gimp_pixel_rgn_init (&map_y_rgn, map_y, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1361.    if (gimp_drawable_has_alpha(map_y->id))
  1362.     ym_alpha = 1;
  1363.    ym_bytes = gimp_drawable_bpp(map_y->id);
  1364.  
  1365.  
  1366.    if (dvals.mag_use == TRUE) {
  1367.      gimp_pixel_rgn_init (&mag_rgn, mag_draw, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1368.      if (gimp_drawable_has_alpha(mag_draw->id))
  1369.        mmag_alpha = 1;
  1370.      mmag_bytes = gimp_drawable_bpp(mag_draw->id);
  1371.  
  1372.      pr = gimp_pixel_rgns_register (5, &src_rgn, &dest_rgn, &map_x_rgn, &map_y_rgn, &mag_rgn);
  1373.    } else {
  1374.      pr = gimp_pixel_rgns_register (4, &src_rgn, &dest_rgn, &map_x_rgn, &map_y_rgn);
  1375.    }
  1376.  
  1377.    dscalefac = (dvals.amount) / (256* 127.5 * dvals.substeps);  /* substep displacement vector scale factor */
  1378.  
  1379.  
  1380.    for (pr = pr; pr != NULL; pr = gimp_pixel_rgns_process (pr))
  1381.     {  
  1382.  
  1383.       srcrow = src_rgn.data;
  1384.       destrow = dest_rgn.data;
  1385.       mxrow = map_x_rgn.data;
  1386.       myrow = map_y_rgn.data;
  1387.       if (dvals.mag_use == TRUE) 
  1388.     mmagrow = mag_rgn.data;
  1389.             
  1390.       /* loop over destination pixels */
  1391.       for (y = dest_rgn.y; y < (dest_rgn.y + dest_rgn.h); y++)
  1392.     {
  1393.       src = srcrow;
  1394.       dest = destrow;
  1395.       mx = mxrow;
  1396.       my = myrow;
  1397.  
  1398.           if (dvals.mag_use == TRUE) 
  1399.         mmag = mmagrow;
  1400.       
  1401.       for (x = dest_rgn.x; x < (dest_rgn.x + dest_rgn.w); x++)
  1402.         {
  1403.           /* ----- Find displacement vector (amnt_x, amnt_y) ------------ */
  1404.  
  1405.               dx = dscalefac * ((256.0*mx[0])+mx[1] -32768);  /* 16-bit values */ 
  1406.           dy = dscalefac * ((256.0*my[0])+my[1] -32768);
  1407.  
  1408.           if (dvals.mag_use == TRUE) {
  1409.         scalefac = warp_map_mag_give_value(mmag, mmag_alpha, mmag_bytes)/255.0;
  1410.         dx *= scalefac;
  1411.         dy *= scalefac;
  1412.           }
  1413.           
  1414.           if (dvals.dither != 0.0) {       /* random dither is +/- dvals.dither pixels */
  1415. #ifdef G_OS_WIN32
  1416.         dx += dvals.dither*((gdouble)(RAND_FUNC () - (G_MAXRAND >> 1)) / (G_MAXRAND >> 1));
  1417.         dy += dvals.dither*((gdouble)(RAND_FUNC () - (G_MAXRAND >> 1)) / (G_MAXRAND >> 1));
  1418. #else
  1419.         dx += dvals.dither*((gdouble)(rand() - (G_MAXRAND >> 1)) / (G_MAXRAND >> 1));
  1420.         dy += dvals.dither*((gdouble)(rand() - (G_MAXRAND >> 1)) / (G_MAXRAND >> 1));
  1421. #endif
  1422.           }
  1423.           
  1424.           if (dvals.substeps != 1) {   /* trace (substeps) iterations of displacement vector */
  1425.  
  1426.         for (substep = 1; substep < dvals.substeps; substep++) {
  1427.  
  1428.                 needx = x + dx;   /* In this (substep) loop, (x,y) remain fixed. (dx,dy) vary each step. */
  1429.               needy = y + dy;
  1430.  
  1431.               if (needx >= 0.0)    xi = (int) needx;
  1432.             else           xi = -((int) -needx + 1);
  1433.  
  1434.           if (needy >= 0.0)    yi = (int) needy;
  1435.             else               yi = -((int) -needy + 1);
  1436.  
  1437.              /* get 4 neighboring DX values from DiffX drawable for linear interpolation */
  1438.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi, yi, &xrow, &xcol, pixel[0]);
  1439.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi + 1, yi, &xrow, &xcol, pixel[1]);
  1440.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi, yi + 1, &xrow, &xcol, pixel[2]);
  1441.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &xrow, &xcol, pixel[3]);
  1442.           
  1443.               ivalues[0] = 256*pixel[0][0] + pixel[0][1];
  1444.           ivalues[1] = 256*pixel[1][0] + pixel[1][1];
  1445.           ivalues[2] = 256*pixel[2][0] + pixel[2][1];
  1446.           ivalues[3] = 256*pixel[3][0] + pixel[3][1];
  1447.           xval = bilinear16(needx, needy, ivalues);
  1448.  
  1449.              /* get 4 neighboring DY values from DiffY drawable for linear interpolation */
  1450.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi, yi, &yrow, &ycol, pixel[0]);
  1451.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi + 1, yi, &yrow, &ycol, pixel[1]);
  1452.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi, yi + 1, &yrow, &ycol, pixel[2]);
  1453.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &yrow, &ycol, pixel[3]);
  1454.           
  1455.               ivalues[0] = 256*pixel[0][0] + pixel[0][1];
  1456.           ivalues[1] = 256*pixel[1][0] + pixel[1][1];
  1457.           ivalues[2] = 256*pixel[2][0] + pixel[2][1];
  1458.           ivalues[3] = 256*pixel[3][0] + pixel[3][1];
  1459.           yval = bilinear16(needx, needy, ivalues);
  1460.  
  1461.           dx += dscalefac * (xval-32768);      /* move displacement vector to this new value */
  1462.           dy += dscalefac * (yval-32768);
  1463.  
  1464.         } /* for (substep) */
  1465.           } /* if (substeps != 0) */
  1466.  
  1467.  
  1468.           /* --------------------------------------------------------- */
  1469.  
  1470.           needx = x + dx;
  1471.           needy = y + dy;
  1472.  
  1473.           mx += xm_bytes;         /* pointers into x,y displacement maps */
  1474.           my += ym_bytes;
  1475.  
  1476.           if (dvals.mag_use == TRUE)
  1477.         mmag += mmag_bytes;
  1478.           
  1479.           /* Calculations complete; now copy the proper pixel */
  1480.           
  1481.           if (needx >= 0.0)
  1482.               xi = (int) needx;
  1483.           else
  1484.               xi = -((int) -needx + 1);
  1485.  
  1486.           if (needy >= 0.0)
  1487.               yi = (int) needy;
  1488.           else
  1489.               yi = -((int) -needy + 1);
  1490.           
  1491.           /* get 4 neighboring pixel values from source drawable for linear interpolation */
  1492.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi, yi, &row, &col, pixel[0]);
  1493.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi + 1, yi, &row, &col, pixel[1]);
  1494.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi, yi + 1, &row, &col, pixel[2]);
  1495.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &row, &col, pixel[3]);
  1496.           
  1497.           for (k = 0; k < dest_bytes; k++)
  1498.         {
  1499.           values[0] = pixel[0][k];
  1500.           values[1] = pixel[1][k];
  1501.           values[2] = pixel[2][k];
  1502.           values[3] = pixel[3][k];
  1503.           val = bilinear(needx, needy, values);
  1504.           
  1505.           *dest++ = val;
  1506.         } /* for k */
  1507.  
  1508.         } /* for x */
  1509.       
  1510.       /*      srcrow += src_rgn.rowstride; */
  1511.       srcrow += src_rgn.rowstride;
  1512.       destrow += dest_rgn.rowstride;
  1513.       mxrow += map_x_rgn.rowstride;
  1514.       myrow += map_y_rgn.rowstride;
  1515.           if (dvals.mag_use == TRUE)
  1516.         mmagrow += mag_rgn.rowstride;
  1517.  
  1518.     } /* for y */
  1519.  
  1520.       progress += (dest_rgn.w * dest_rgn.h);
  1521.       gimp_progress_update ((double) progress / (double) max_progress);
  1522.  
  1523.   
  1524.     } /* for pr */
  1525.  
  1526.    if (tile != NULL)
  1527.     gimp_tile_unref (tile, FALSE);
  1528.  
  1529.    if (xtile != NULL)
  1530.     gimp_tile_unref (xtile, FALSE);
  1531.  
  1532.    if (ytile != NULL)
  1533.     gimp_tile_unref (ytile, FALSE);
  1534.  
  1535.      /*  update the region  */
  1536.    gimp_drawable_flush (new);
  1537.  
  1538.    gimp_drawable_merge_shadow(draw->id, (first_time == TRUE));
  1539.   
  1540. } /* warp_one */
  1541.  
  1542. /* ------------------------------------------------------------------------- */
  1543.  
  1544. static gdouble
  1545. warp_map_mag_give_value (guchar *pt,
  1546.              gint    alpha,
  1547.              gint    bytes)
  1548. {
  1549.   gdouble ret, val_alpha;
  1550.   
  1551.   if (bytes >= 3)
  1552.     ret =  (pt[0] + pt[1] + pt[2])/3.0;
  1553.   else
  1554.     ret = (gdouble) *pt;
  1555.   
  1556.   if (alpha)
  1557.     {
  1558.       val_alpha = pt[bytes - 1];
  1559.       ret = (ret * val_alpha / 255.0);
  1560.     };
  1561.   
  1562.   return (ret);
  1563. }
  1564.  
  1565.  
  1566. static GimpTile *
  1567. warp_pixel (GimpDrawable *drawable,
  1568.         GimpTile     *tile,
  1569.         gint       width,
  1570.         gint       height,
  1571.         gint       x1,
  1572.         gint       y1,
  1573.         gint       x2,
  1574.         gint       y2,
  1575.         gint       x,
  1576.         gint       y,
  1577.         gint      *row,
  1578.         gint      *col,
  1579.         guchar    *pixel)
  1580. {
  1581.   static guchar empty_pixel[4] = {0, 0, 0, 0};
  1582.   guchar *data;
  1583.   gint b;
  1584.  
  1585.   /* Tile the image. */
  1586.   if (dvals.wrap_type == WRAP)
  1587.     {
  1588.       if (x < 0)
  1589.     x = width - (-x % width);
  1590.       else
  1591.     x %= width;
  1592.  
  1593.       if (y < 0)
  1594.     y = height - (-y % height);
  1595.       else
  1596.     y %= height;
  1597.     }
  1598.   /* Smear out the edges of the image by repeating pixels. */
  1599.   else if (dvals.wrap_type == SMEAR)
  1600.     {
  1601.       if (x < 0)
  1602.     x = 0;
  1603.       else if (x > width - 1)
  1604.     x = width - 1;
  1605.  
  1606.       if (y < 0)
  1607.     y = 0;
  1608.       else if (y > height - 1)
  1609.     y = height - 1;
  1610.     }
  1611.  
  1612.   if (x >= x1 && y >= y1 && x < x2 && y < y2)
  1613.     {
  1614.       if ((( (guint) (x / tile_width)) != *col) || (( (guint) (y / tile_height)) != *row))
  1615.     {
  1616.       *col = x / tile_width;
  1617.       *row = y / tile_height;
  1618.       if (tile)
  1619.         gimp_tile_unref (tile, FALSE);
  1620.       tile = gimp_drawable_get_tile (drawable, FALSE, *row, *col);
  1621.       gimp_tile_ref (tile);
  1622.     }
  1623.  
  1624.       data = tile->data + tile->bpp * (tile->ewidth * (y % tile_height) + (x % tile_width));
  1625.     }
  1626.   else
  1627.     {
  1628.       if (dvals.wrap_type == BLACK)
  1629.         data = empty_pixel;
  1630.       else
  1631.         data = color_pixel;      /* must have selected COLOR type */
  1632.     }
  1633.  
  1634.   for (b = 0; b < drawable->bpp; b++)
  1635.     pixel[b] = data[b];
  1636.  
  1637.   return tile;
  1638. }
  1639.  
  1640. static guchar
  1641. bilinear (gdouble  x,
  1642.       gdouble  y,
  1643.       guchar  *v)
  1644. {
  1645.   gdouble m0, m1;
  1646.  
  1647.   x = fmod(x, 1.0);
  1648.   y = fmod(y, 1.0);
  1649.  
  1650.   if (x < 0)
  1651.     x += 1.0;
  1652.   if (y < 0)
  1653.     y += 1.0;
  1654.  
  1655.   m0 = (gdouble) v[0] + x * ((gdouble) v[1] - v[0]);
  1656.   m1 = (gdouble) v[2] + x * ((gdouble) v[3] - v[2]);
  1657.  
  1658.   return (guchar) (m0 + y * (m1 - m0));
  1659. } /* bilinear */
  1660.  
  1661. static gint
  1662. bilinear16 (gdouble  x,
  1663.         gdouble  y,
  1664.         gint    *v)
  1665. {
  1666.   gdouble m0, m1;
  1667.  
  1668.   x = fmod(x, 1.0);
  1669.   y = fmod(y, 1.0);
  1670.  
  1671.   if (x < 0)
  1672.     x += 1.0;
  1673.   if (y < 0)
  1674.     y += 1.0;
  1675.  
  1676.   m0 = (gdouble) v[0] + x * ((gdouble) v[1] - v[0]);
  1677.   m1 = (gdouble) v[2] + x * ((gdouble) v[3] - v[2]);
  1678.  
  1679.   return (gint) (m0 + y * (m1 - m0));
  1680. } /* bilinear16 */
  1681.  
  1682.  
  1683. /*  Warp interface functions  */
  1684.  
  1685. static gint
  1686. warp_map_constrain (gint32     image_id,
  1687.             gint32     drawable_id,
  1688.             gpointer   data)
  1689. {
  1690.   GimpDrawable *drawable;
  1691.  
  1692.   drawable = (GimpDrawable *) data;
  1693.  
  1694.   if (drawable_id == -1)
  1695.     return TRUE;
  1696.  
  1697.   if (gimp_drawable_width (drawable_id) == drawable->width &&
  1698.       gimp_drawable_height (drawable_id) == drawable->height)
  1699.     return TRUE;
  1700.   else
  1701.     return FALSE;
  1702. }
  1703.  
  1704. static void
  1705. warp_map_callback (gint32   id,
  1706.            gpointer data)
  1707. {
  1708.   dvals.warp_map = id;
  1709. }
  1710.  
  1711. static void
  1712. warp_map_mag_callback (gint32   id,
  1713.                gpointer data)
  1714. {
  1715.   dvals.mag_map = id;
  1716. }
  1717.  
  1718. static void
  1719. warp_map_grad_callback (gint32   id,
  1720.             gpointer data)
  1721. {
  1722.   dvals.grad_map = id;
  1723. }
  1724.  
  1725. static void
  1726. warp_map_vector_callback (gint32   id,
  1727.               gpointer data)
  1728. {
  1729.   dvals.vector_map = id;
  1730. }
  1731.  
  1732. static void
  1733. warp_ok_callback (GtkWidget *widget,
  1734.           gpointer   data)
  1735. {
  1736.   dint.run = TRUE;
  1737.  
  1738.   gtk_widget_destroy (GTK_WIDGET (data));
  1739. }
  1740.